package app.server.project.main.service.db.dao.user;

import app.server.project.core.constants.Def;
import app.server.project.main.service.db.entity.user.UserLuckOrderEntity;
import app.server.project.main.service.db.mapper.user.UserLuckOrderMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Repository
public class UserLuckOrderDao extends ServiceImpl<UserLuckOrderMapper, UserLuckOrderEntity> {

    @Cacheable(key = "#userId+':'+#type", cacheNames = "app.server.project.main.service.db.dao.UserLuckOrderDao")
    public List<UserLuckOrderEntity> getAll(long userId, int type, long size) {
        IPage<UserLuckOrderEntity> page = Page.of(1, size);
        var dbs = this.page(page, Wrappers.lambdaQuery(UserLuckOrderEntity.class)
                .eq(UserLuckOrderEntity::getUserId, userId)
                .eq(UserLuckOrderEntity::getOrderType, type)
                .ne(UserLuckOrderEntity::getSuccessUnixTime, Def.Zero)
                .orderByDesc(UserLuckOrderEntity::getSuccessUnixTime));
        var ret = new ArrayList<UserLuckOrderEntity>(dbs.getRecords());
        ret.forEach(item -> {
            item.setSynDb(true);
        });
        return ret;
    }

    public List<UserLuckOrderEntity> getAllLuck(int type, long size) {
        IPage<UserLuckOrderEntity> page = Page.of(1, size);
        var dbs = this.page(page, Wrappers.lambdaQuery(UserLuckOrderEntity.class)
                .eq(UserLuckOrderEntity::getOrderType, type)
                .ne(UserLuckOrderEntity::getTonVal, Def.Zero)
                .orderByDesc(UserLuckOrderEntity::getSuccessUnixTime));
        var ret = new ArrayList<UserLuckOrderEntity>(dbs.getRecords());
        ret.forEach(item -> {
            item.setSynDb(true);
        });
        return ret;
    }

    public List<UserLuckOrderEntity> getAllUnSuccess(int type) {
        var dbs = this.list(Wrappers.lambdaQuery(UserLuckOrderEntity.class)
                .ne(UserLuckOrderEntity::getSuccessUnixTime, Def.Zero)
                .eq(UserLuckOrderEntity::getStatus, Def.FALSE)
                .eq(UserLuckOrderEntity::getOrderType, type));
        dbs.forEach(item -> {
            item.setSynDb(true);
        });
        return dbs;
    }

    @Cacheable(key = "#orderId", cacheNames = "app.server.project.main.service.db.dao.getOrder")
    public UserLuckOrderEntity getOrder(long orderId) {
        var data = this.getById(orderId);
        if (Objects.isNull(data)) {
            return new UserLuckOrderEntity();
        }
        data.setSynDb(true);
        return data;
    }

    @CachePut(key = "#entity.id", cacheNames = "app.server.project.main.service.db.dao.getOrder")
    public UserLuckOrderEntity updateOrder(UserLuckOrderEntity entity) {
        return entity;
    }

}
